package guda.house.biz.security;

import guda.grape.mvc.spring.SpringBeanFactoryTool;
import guda.grape.util.Convert;
import guda.house.biz.beans.vo.VillageVO;
import guda.house.biz.enums.BooleanEnum;
import guda.house.biz.query.BankChargeRuleQuery;
import guda.house.biz.query.OwnerQuery;
import guda.house.dal.dao.*;
import guda.house.dal.dataobject.*;

import javax.swing.*;
import java.util.List;

/**
 * Created by well on 15/11/14.
 */
public class AppUtil {

    public static String getVillageName(Object villageId) {
        List<VillageVO> villageVOList = AuthUser.getAuthHolder().get().getVillageVOList();
        if (villageVOList == null) {
            return null;
        }
        for (VillageVO villageVO : villageVOList) {
            if (villageVO.getVillageId().equals(villageId)) {
                return villageVO.getVillageName();
            }
        }
        return null;
    }

    public static List<VillageVO> getVillageList() {
        return AuthUser.getAuthHolder().get().getVillageVOList();

    }

    public static List<Long> getVillageIdList() {
        return AuthUser.getAuthHolder().get().getVillageIdList();

    }

    public static BuildingDO getBuildingById(Object buildingId) {
        BuildingDAO buildingDAO = SpringBeanFactoryTool.getBeanFactory().getBean(BuildingDAO.class);
        return buildingDAO.getBuildingById(Convert.toLong(buildingId));

    }

    public static SuiteDO getSuiteById(Object suiteId) {
        SuiteDAO suiteDAO = SpringBeanFactoryTool.getBeanFactory().getBean(SuiteDAO.class);
        return suiteDAO.getSuiteById(Convert.toLong(suiteId));

    }

    public static UnitDO getUnitById(Object unitId) {
        UnitDAO unitDAO = SpringBeanFactoryTool.getBeanFactory().getBean(UnitDAO.class);
        return unitDAO.getUnitById(Convert.toLong(unitId));

    }

    public static VillageDO getVillageBySuiteId(Object suiteId) {
        SuiteDAO suiteDAO = SpringBeanFactoryTool.getBeanFactory().getBean(SuiteDAO.class);
        SuiteDO suiteById = suiteDAO.getSuiteById(Convert.toLong(suiteId));

        UnitDAO unitDAO = SpringBeanFactoryTool.getBeanFactory().getBean(UnitDAO.class);
        UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());

        BuildingDAO buildingDAO = SpringBeanFactoryTool.getBeanFactory().getBean(BuildingDAO.class);
        BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());

        VillageDAO villageDAO = SpringBeanFactoryTool.getBeanFactory().getBean(VillageDAO.class);
        return villageDAO.getVillageById(buildingById.getVillageId());
    }

    public static BuildingDO getBuildingBySuiteId(Object suiteId) {
        SuiteDAO suiteDAO = SpringBeanFactoryTool.getBeanFactory().getBean(SuiteDAO.class);
        SuiteDO suiteById = suiteDAO.getSuiteById(Convert.toLong(suiteId));

        UnitDAO unitDAO = SpringBeanFactoryTool.getBeanFactory().getBean(UnitDAO.class);
        UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());

        BuildingDAO buildingDAO = SpringBeanFactoryTool.getBeanFactory().getBean(BuildingDAO.class);
        return buildingDAO.getBuildingById(unitById.getBuildingId());
    }

    public static SuiteDO getSuiteBySuiteId(Object suiteId) {
        SuiteDAO suiteDAO = SpringBeanFactoryTool.getBeanFactory().getBean(SuiteDAO.class);
        return suiteDAO.getSuiteById(Convert.toLong(suiteId));

    }
    public static UnitDO getUnitBySuiteId(Object suiteId) {
        SuiteDAO suiteDAO = SpringBeanFactoryTool.getBeanFactory().getBean(SuiteDAO.class);
        SuiteDO suiteById = suiteDAO.getSuiteById(Convert.toLong(suiteId));

        UnitDAO unitDAO = SpringBeanFactoryTool.getBeanFactory().getBean(UnitDAO.class);
        return unitDAO.getUnitById(suiteById.getUnitId());

    }

    public static SysVarDO getSysVar(Object varId) {
        SysVarDAO sysVarDAO = SpringBeanFactoryTool.getBeanFactory().getBean(SysVarDAO.class);
        return sysVarDAO.getSysVarById(Convert.toLong(varId));


    }

    public static Long countDeposit(Object varId,Object villageId,Object money) {
        BankChargeRuleDAO bankChargeRuleDAO = SpringBeanFactoryTool.getBeanFactory().getBean(BankChargeRuleDAO.class);

        BankChargeRuleQuery bankChargeRuleQuery = new BankChargeRuleQuery();
        bankChargeRuleQuery.setVillageId(Long.parseLong(String.valueOf(villageId)));
        bankChargeRuleQuery.setVarId(Long.parseLong(String.valueOf(varId)));


        List<BankChargeRuleDO> bankChargeRuleDOs = bankChargeRuleDAO.selectForPage(bankChargeRuleQuery);
        if(bankChargeRuleDOs.size() == 0){
            return null;
        }
        BankChargeRuleDO bankChargeRuleDO = bankChargeRuleDOs.get(0);
        Integer chargeUnit = bankChargeRuleDO.getChargeUnit();
        long price = Long.parseLong(String.valueOf(money));
        long total =  price * chargeUnit /10000;
        if(bankChargeRuleDO.getMaxValue() != null){
            if(price > bankChargeRuleDO.getMaxValue().intValue()){
                total = Long.valueOf(bankChargeRuleDO.getMaxFee());
            }
        }
        return total;

    }

    public static boolean isEmployee(Object suiteId){
        if(suiteId == null){
            return false;
        }
        OwnerDAO ownerDAO = SpringBeanFactoryTool.getBeanFactory().getBean(OwnerDAO.class);
        OwnerQuery ownerQuery = new OwnerQuery();
        ownerQuery.setSuiteId(Long.parseLong(String.valueOf(suiteId)));
        List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
        for(OwnerDO ownerDO:ownerDOs){
            if(ownerDO.getIsEmployee()!= null && ownerDO.getIsEmployee().intValue() == BooleanEnum.TRUE.getVal()){
                return true;
            }
        }
        return false;
    }
}
